home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / text / faqs / unix-faq.faq.part3 < prev    next >
Encoding:
Internet Message Format  |  1994-05-16  |  30.9 KB

  1. Subject: Unix - Frequently Asked Questions (3/7) [Frequent posting]
  2. Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
  3. From: tmatimar@isgtec.com (Ted Timar)
  4. Date: 14 May 1994 10:18:45 GMT
  5.  
  6. Archive-name: unix-faq/faq/part3
  7. Version: $Id: part3,v 2.5 1994/04/28 19:25:03 tmatimar Exp $
  8.  
  9. These seven articles contain the answers to some Frequently Asked
  10. Questions often seen in comp.unix.questions and comp.unix.shell.
  11. Please don't ask these questions again, they've been answered plenty
  12. of times already - and please don't flame someone just because they may
  13. not have read this particular posting.  Thank you.
  14.  
  15. This collection of documents is Copyright (c) 1994, Ted Timar.
  16. All rights reserved.  Permission to distribute the collection is
  17. hereby granted providing that distribution is electronic, no money
  18. is involved, reasonable attempts are made to use the latest version
  19. and all credits and this copyright notice are maintained.
  20. Other requests for distribution will be considered.
  21.  
  22. All information here has been contributed with good intentions, but
  23. none of it is guaranteed either by the contributors or myself to be
  24. accurate.  The users of this information take all responsibility for
  25. any damage that may occur.
  26.  
  27. Many FAQs, including this one, are available on the archive site
  28. rtfm.mit.edu in the directory pub/usenet/news.answers.
  29. The name under which a FAQ is archived appears in the "Archive-Name:"
  30. line at the top of the article.  This FAQ is archived as
  31. "unix-faq/faq/part[1-7]".
  32.  
  33. These articles are divided approximately as follows:
  34.  
  35.       1.*) General questions.
  36.       2.*) Relatively basic questions, likely to be asked by beginners.
  37.       3.*) Intermediate questions.
  38.       4.*) Advanced questions, likely to be asked by people who thought
  39.        they already knew all of the answers.
  40.       5.*) Questions pertaining to the various shells, and the differences.
  41.       6.*) An overview of Unix variants.
  42.       7.*) An comparison of configuration management systems (RCS, SCCS).
  43.  
  44. This article includes answers to:
  45.  
  46.       3.1)  How do I find the creation time of a file?
  47.       3.2)  How do I use "rsh" without having the rsh hang around
  48.               until the remote command has completed?
  49.       3.3)  How do I truncate a file?
  50.       3.4)  Why doesn't find's "{}" symbol do what I want?
  51.       3.5)  How do I set the permissions on a symbolic link?
  52.       3.6)  How do I "undelete" a file?
  53.       3.7)  How can a process detect if it's running in the background?
  54.       3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  55.       3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  56.               programs from a shell script or in the background?
  57.       3.10) How do I find the process ID of a program with a particular
  58.             name from inside a shell script or C program?
  59.       3.11) How do I check the exit status of a remote command
  60.             executed via "rsh" ?
  61.       3.12) Is it possible to pass shell variable settings into an awk program?
  62.       3.13) How do I get rid of zombie processes that persevere?
  63.       3.14) How do I get lines from a pipe as they are written instead of
  64.             only in larger blocks.
  65.  
  66. If you're looking for the answer to, say, question 3.5, and want to skip
  67. everything else, you can search ahead for the regular expression "^3.5)".
  68.  
  69. While these are all legitimate questions, they seem to crop up in
  70. comp.unix.questions or comp.unix.shell on an annual basis, usually
  71. followed by plenty of replies (only some of which are correct) and then
  72. a period of griping about how the same questions keep coming up.  You
  73. may also like to read the monthly article "Answers to Frequently Asked
  74. Questions" in the newsgroup "news.announce.newusers", which will tell
  75. you what "UNIX" stands for.
  76.  
  77. With the variety of Unix systems in the world, it's hard to guarantee
  78. that these answers will work everywhere.  Read your local manual pages
  79. before trying anything suggested here.  If you have suggestions or
  80. corrections for any of these answers, please send them to to
  81. tmatimar@isgtec.com.
  82.  
  83. ----------------------------------------------------------------------
  84.  
  85. Subject: How do I find the creation time of a file?
  86. Date: Thu Mar 18 17:16:55 EST 1993
  87.  
  88. 3.1)  How do I find the creation time of a file?
  89.  
  90.       You can't - it isn't stored anywhere.  Files have a last-modified
  91.       time (shown by "ls -l"), a last-accessed time (shown by "ls -lu")
  92.       and an inode change time (shown by "ls -lc"). The latter is often
  93.       referred to as the "creation time" - even in some man pages -
  94.       but that's wrong; it's also set by such operations as mv, ln,
  95.       chmod, chown and chgrp.
  96.  
  97.       The man page for "stat(2)" discusses this.
  98.  
  99. ------------------------------
  100.  
  101. Subject: How do I use "rsh" without having the rsh hang around ... ?
  102. Date: Thu Mar 18 17:16:55 EST 1993
  103.  
  104. 3.2)  How do I use "rsh" without having the rsh hang around until the
  105.       remote command has completed?
  106.  
  107.       (See note in question 2.7 about what "rsh" we're talking about.)
  108.  
  109.       The obvious answers fail:
  110.               rsh machine command &
  111.       or      rsh machine 'command &'
  112.  
  113.       For instance, try doing   rsh machine 'sleep 60 &' and you'll see
  114.       that the 'rsh' won't exit right away.  It will wait 60 seconds
  115.       until the remote 'sleep' command finishes, even though that
  116.       command was started in the background on the remote machine.  So
  117.       how do you get the 'rsh' to exit immediately after the 'sleep' is
  118.       started?
  119.  
  120.       The solution - if you use csh on the remote machine:
  121.  
  122.         rsh machine -n 'command >&/dev/null </dev/null &'
  123.  
  124.       If you use sh on the remote machine:
  125.  
  126.         rsh machine -n 'command >/dev/null 2>&1 </dev/null &'
  127.  
  128.       Why?  "-n" attaches rsh's stdin to /dev/null so you could run the
  129.       complete rsh command in the background on the LOCAL machine.
  130.       Thus "-n" is equivalent to another specific "< /dev/null".
  131.       Furthermore, the input/output redirections on the REMOTE machine
  132.       (inside the single quotes) ensure that rsh thinks the session can
  133.       be terminated (there's no data flow any more.)
  134.  
  135.       Note: The file that you redirect to/from on the remote machine
  136.       doesn't have to be /dev/null; any ordinary file will do.
  137.  
  138.       In many cases, various parts of these complicated commands
  139.       aren't necessary.
  140.  
  141. ------------------------------
  142.  
  143. Subject: How do I truncate a file?
  144. Date: Thu Mar 18 17:16:55 EST 1993
  145.  
  146. 3.3)  How do I truncate a file?
  147.  
  148.       The BSD function ftruncate() sets the length of a file.  Xenix -
  149.       and therefore SysV r3.2 and later - has the chsize() system
  150.       call.  For other systems, the only kind of truncation you can do
  151.       is truncation to length zero with creat() or open(..., O_TRUNC).
  152.  
  153. ------------------------------
  154.  
  155. Subject: Why doesn't find's "{}" symbol do what I want?
  156. Date: Thu Mar 18 17:16:55 EST 1993
  157.  
  158. 3.4)  Why doesn't find's "{}" symbol do what I want?
  159.  
  160.       "find" has a -exec option that will execute a particular command
  161.       on all the selected files. Find will replace any "{}" it sees
  162.       with the name of the file currently under consideration.
  163.  
  164.       So, some day you might try to use "find" to run a command on
  165.       every file, one directory at a time.  You might try this:
  166.  
  167.     find /path -type d -exec command {}/\* \;
  168.  
  169.       hoping that find will execute, in turn
  170.  
  171.     command directory1/*
  172.     command directory2/*
  173.     ...
  174.  
  175.       Unfortunately, find only expands the "{}" token when it appears
  176.       by itself.  Find will leave anything else like "{}/*" alone, so
  177.       instead of doing what you want, it will do
  178.  
  179.     command {}/*
  180.     command {}/*
  181.     ...
  182.  
  183.       once for each directory.  This might be a bug, it might be a
  184.       feature, but we're stuck with the current behaviour.
  185.  
  186.       So how do you get around this?  One way would be to write a
  187.       trivial little shell script, let's say "./doit", that consists of
  188.  
  189.     command "$1"/*
  190.  
  191.       You could then use
  192.  
  193.     find /path -type d -exec ./doit {} \;
  194.  
  195.       Or if you want to avoid the "./doit" shell script, you can use
  196.  
  197.     find /path -type d -exec sh -c 'command $0/*' {} \;
  198.  
  199.       (This works because within the 'command' of "sh -c 'command' A B C ...",
  200.        $0 expands to A, $1 to B, and so on.)
  201.  
  202.       or you can use the construct-a-command-with-sed trick
  203.  
  204.     find /path -type d -print | sed 's:.*:command &/*:' | sh
  205.  
  206.       If all you're trying to do is cut down on the number of times
  207.       that "command" is executed, you should see if your system has the
  208.       "xargs" command.  Xargs reads arguments one line at a time from
  209.       the standard input and assembles as many of them as will fit into
  210.       one command line.  You could use
  211.  
  212.     find /path -print | xargs command
  213.  
  214.       which would result in one or more executions of
  215.  
  216.     command file1 file2 file3 file4 dir1/file1 dir1/file2
  217.  
  218.       Unfortunately this is not a perfectly robust or secure solution.
  219.       Xargs expects its input lines to be terminated with newlines, so
  220.       it will be confused by files with odd characters such as newlines
  221.       in their names.
  222.  
  223. ------------------------------
  224.  
  225. Subject: How do I set the permissions on a symbolic link?
  226. Date: Thu Mar 18 17:16:55 EST 1993
  227.  
  228. 3.5)  How do I set the permissions on a symbolic link?
  229.  
  230.       Permissions on a symbolic link don't really mean anything.  The
  231.       only permissions that count are the permissions on the file that
  232.       the link points to.
  233.  
  234. ------------------------------
  235.  
  236. Subject: How do I "undelete" a file?
  237. Date: Thu Mar 18 17:16:55 EST 1993
  238.  
  239. 3.6)  How do I "undelete" a file?
  240.  
  241.       Someday, you are going to accidentally type something like
  242.       "rm * .foo", and find you just deleted "*" instead of "*.foo".
  243.       Consider it a rite of passage.
  244.  
  245.       Of course, any decent systems administrator should be doing
  246.       regular backups.  Check with your sysadmin to see if a recent
  247.       backup copy of your file is available.  But if it isn't, read
  248.       on.
  249.  
  250.       For all intents and purposes, when you delete a file with "rm" it
  251.       is gone.  Once you "rm" a file, the system totally forgets which
  252.       blocks scattered around the disk were part of your file.  Even
  253.       worse, the blocks from the file you just deleted are going to be
  254.       the first ones taken and scribbled upon when the system needs
  255.       more disk space.  However, never say never.  It is theoretically
  256.       possible *if* you shut down the system immediately after the "rm"
  257.       to recover portions of the data.  However, you had better have a
  258.       very wizardly type person at hand with hours or days to spare to
  259.       get it all back.
  260.  
  261.       Your first reaction when you "rm" a file by mistake is why not
  262.       make a shell alias or procedure which changes "rm" to move files
  263.       into a trash bin rather than delete them?  That way you can
  264.       recover them if you make a mistake, and periodically clean out
  265.       your trash bin.  Two points:  first, this is generally accepted
  266.       as a *bad* idea.  You will become dependent upon this behaviour
  267.       of "rm", and you will find yourself someday on a normal system
  268.       where "rm" is really "rm", and you will get yourself in trouble.
  269.       Second, you will eventually find that the hassle of dealing with
  270.       the disk space and time involved in maintaining the trash bin, it
  271.       might be easier just to be a bit more careful with "rm".  For
  272.       starters, you should look up the "-i" option to "rm" in your
  273.       manual.
  274.  
  275.       If you are still undaunted, then here is a possible simple
  276.       answer.  You can create yourself a "can" command which moves
  277.       files into a trashcan directory. In csh(1) you can place the
  278.       following commands in the ".login" file in your home directory:
  279.  
  280.     alias can    'mv \!* ~/.trashcan'       # junk file(s) to trashcan
  281.     alias mtcan    'rm -f ~/.trashcan/*'       # irretrievably empty trash
  282.     if ( ! -d ~/.trashcan ) mkdir ~/.trashcan  # ensure trashcan exists
  283.  
  284.       You might also want to put a:
  285.  
  286.     rm -f ~/.trashcan/*
  287.  
  288.       in the ".logout" file in your home directory to automatically
  289.       empty the trash when you log out.  (sh and ksh versions are left
  290.       as an exercise for the reader.)
  291.  
  292.       MIT's Project Athena has produced a comprehensive
  293.       delete/undelete/expunge/purge package, which can serve as a
  294.       complete replacement for rm which allows file recovery.  This
  295.       package was posted to comp.sources.misc (volume 17, issue
  296.       023-026)
  297.  
  298. ------------------------------
  299.  
  300. Subject: How can a process detect if it's running in the background?
  301. Date: Thu Mar 18 17:16:55 EST 1993
  302.  
  303. 3.7)  How can a process detect if it's running in the background?
  304.  
  305.       First of all: do you want to know if you're running in the
  306.       background, or if you're running interactively? If you're
  307.       deciding whether or not you should print prompts and the like,
  308.       that's probably a better criterion. Check if standard input
  309.       is a terminal:
  310.  
  311.         sh: if [ -t 0 ]; then ... fi
  312.         C: if(isatty(0)) { ... }
  313.  
  314.       In general, you can't tell if you're running in the background.
  315.       The fundamental problem is that different shells and different
  316.       versions of UNIX have different notions of what "foreground" and
  317.       "background" mean - and on the most common type of system with a
  318.       better-defined notion of what they mean, programs can be moved
  319.       arbitrarily between foreground and background!
  320.  
  321.       UNIX systems without job control typically put a process into the
  322.       background by ignoring SIGINT and SIGQUIT and redirecting the
  323.       standard input to "/dev/null"; this is done by the shell.
  324.  
  325.       Shells that support job control, on UNIX systems that support job
  326.       control, put a process into the background by giving it a process
  327.       group ID different from the process group to which the terminal
  328.       belongs.  They move it back into the foreground by setting the
  329.       terminal's process group ID to that of the process.  Shells that
  330.       do *not* support job control, on UNIX systems that support job
  331.       control, typically do what shells do on systems that don't
  332.       support job control.
  333.  
  334. ------------------------------
  335.  
  336. Subject: Why doesn't redirecting a loop work as intended?  (Bourne shell)
  337. Date: Thu Mar 18 17:16:55 EST 1993
  338.  
  339. 3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  340.  
  341.       Take the following example:
  342.  
  343.     foo=bar
  344.  
  345.     while read line
  346.     do
  347.         # do something with $line
  348.         foo=bletch
  349.     done < /etc/passwd
  350.  
  351.     echo "foo is now: $foo"
  352.  
  353.       Despite the assignment ``foo=bletch'' this will print
  354.       ``foo is now: bar'' in many implementations of the Bourne shell.
  355.       Why?  Because of the following, often undocumented, feature of
  356.       historic Bourne shells: redirecting a control structure (such as
  357.       a loop, or an ``if'' statement) causes a subshell to be created,
  358.       in which the structure is executed; variables set in that
  359.       subshell (like the ``foo=bletch'' assignment) don't affect the
  360.       current shell, of course.
  361.  
  362.       The POSIX 1003.2 Shell and Tools Interface standardization
  363.       committee forbids the behaviour described above, i.e. in P1003.2
  364.       conformant Bourne shells the example will print ``foo is now:
  365.       bletch''.
  366.  
  367.       In historic (and P1003.2 conformant) implementations you can use
  368.       the following `trick' to get around the redirection problem:
  369.  
  370.     foo=bar
  371.  
  372.     # make file descriptor 9 a duplicate of file descriptor 0 (stdin);
  373.     # then connect stdin to /etc/passwd; the original stdin is now
  374.     # `remembered' in file descriptor 9; see dup(2) and sh(1)
  375.     exec 9<&0 < /etc/passwd
  376.  
  377.     while read line
  378.     do
  379.         # do something with $line
  380.         foo=bletch
  381.     done
  382.  
  383.     # make stdin a duplicate of file descriptor 9, i.e. reconnect
  384.     # it to the original stdin; then close file descriptor 9
  385.     exec 0<&9 9<&-
  386.  
  387.     echo "foo is now: $foo"
  388.  
  389.       This should always print ``foo is now: bletch''.
  390.       Right, take the next example:
  391.  
  392.     foo=bar
  393.  
  394.     echo bletch | read foo
  395.  
  396.     echo "foo is now: $foo"
  397.  
  398.       This will print ``foo is now: bar'' in many implementations,
  399.       ``foo is now: bletch'' in some others.  Why?  Generally each part
  400.       of a pipeline is run in a different subshell; in some
  401.       implementations though, the last command in the pipeline is made
  402.       an exception: if it is a builtin command like ``read'', the
  403.       current shell will execute it, else another subshell is created.
  404.  
  405.       POSIX 1003.2 allows both behaviours so portable scripts cannot
  406.       depend on any of them.
  407.  
  408. ------------------------------
  409.  
  410. Subject: How do I run ... interactive programs from a shell script ... ?
  411. Date: Thu Mar 18 17:16:55 EST 1993
  412.  
  413. 3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  414.       programs from a shell script or in the background?
  415.  
  416.       These programs expect a terminal interface.  Shells makes no
  417.       special provisions to provide one.  Hence, such programs cannot
  418.       be automated in shell scripts.
  419.  
  420.       The 'expect' program provides a programmable terminal interface
  421.       for automating interaction with such programs.  The following
  422.       expect script is an example of a non-interactive version of
  423.       passwd(1).
  424.  
  425.     # username is passed as 1st arg, password as 2nd
  426.     set password [index $argv 2]
  427.     spawn passwd [index $argv 1]
  428.     expect "*password:"
  429.     send "$password\r"
  430.     expect "*password:"
  431.     send "$password\r"
  432.     expect eof
  433.  
  434.       expect can partially automate interaction which is especially
  435.       useful for telnet, rlogin, debuggers or other programs that have
  436.       no built-in command language.  The distribution provides an
  437.       example script to rerun rogue until a good starting configuration
  438.       appears.  Then, control is given back to the user to enjoy the game.
  439.  
  440.       Fortunately some programs have been written to manage the
  441.       connection to a pseudo-tty so that you can run these sorts of
  442.       programs in a script.
  443.  
  444.       To get expect, email "send pub/expect/expect.shar.Z" to
  445.       library@cme.nist.gov or anonymous ftp same from
  446.       ftp.cme.nist.gov.
  447.  
  448.       Another solution is provided by the pty 4.0 program, which runs a
  449.       program under a pseudo-tty session and was posted to
  450.       comp.sources.unix, volume 25.  A pty-based solution using named
  451.       pipes to do the same as the above might look like this:
  452.  
  453.     #!/bin/sh
  454.     /etc/mknod out.$$ p; exec 2>&1
  455.     ( exec 4<out.$$; rm -f out.$$
  456.     <&4 waitfor 'password:'
  457.         echo "$2"
  458.     <&4 waitfor 'password:'
  459.         echo "$2"
  460.     <&4 cat >/dev/null
  461.     ) | ( pty passwd "$1" >out.$$ )
  462.  
  463.       Here, 'waitfor' is a simple C program that searches for
  464.       its argument in the input, character by character.
  465.  
  466.       A simpler pty solution (which has the drawback of not
  467.       synchronizing properly with the passwd program) is
  468.  
  469.     #!/bin/sh
  470.     ( sleep 5; echo "$2"; sleep 5; echo "$2") | pty passwd "$1"
  471.  
  472. ------------------------------
  473.  
  474. Subject: How do I find the process ID of a program with a particular name ... ?
  475. Date: Thu Mar 18 17:16:55 EST 1993
  476.  
  477. 3.10) How do I find the process ID of a program with a particular name
  478.       from inside a shell script or C program?
  479.  
  480.       In a shell script:
  481.  
  482.       There is no utility specifically designed to map between program
  483.       names and process IDs.  Furthermore, such mappings are often
  484.       unreliable, since it's possible for more than one process to have
  485.       the same name, and since it's possible for a process to change
  486.       its name once it starts running.  However, a pipeline like this
  487.       can often be used to get a list of processes (owned by you) with
  488.       a particular name:
  489.  
  490.         ps ux | awk '/name/ && !/awk/ {print $2}'
  491.  
  492.       You replace "name" with the name of the process for which you are
  493.       searching.
  494.  
  495.       The general idea is to parse the output of ps, using awk or grep
  496.       or other utilities, to search for the lines with the specified
  497.       name on them, and print the PID's for those lines.  Note that the
  498.       "!/awk/" above prevents the awk process for being listed.
  499.  
  500.       You may have to change the arguments to ps, depending on what
  501.       kind of Unix you are using.
  502.  
  503.       In a C program:
  504.  
  505.       Just as there is no utility specifically designed to map between
  506.       program names and process IDs, there are no (portable) C library
  507.       functions to do it either.
  508.  
  509.       However, some vendors provide functions for reading Kernel
  510.       memory; for example, Sun provides the "kvm_" functions, and Data
  511.       General provides the "dg_" functions.  It may be possible for any
  512.       user to use these, or they may only be useable by the super-user
  513.       (or a user in group "kmem") if read-access to kernel memory on
  514.       your system is restricted.  Furthermore, these functions are
  515.       often not documented or documented badly, and might change from
  516.       release to release.
  517.  
  518.       Some vendors provide a "/proc" filesystem, which appears as a
  519.       directory with a bunch of filenames in it.  Each filename is a
  520.       number, corresponding to a process ID, and you can open the file
  521.       and read it to get information about the process.  Once again,
  522.       access to this may be restricted, and the interface to it may
  523.       change from system to system.
  524.  
  525.       If you can't use vendor-specific library functions, and you
  526.       don't have /proc, and you still want to do this completely
  527.       in C, you
  528.       are going to have to do the rummaging through kernel memory
  529.       yourself.  For a good example of how to do this on many systems,
  530.       see the sources to "ofiles", available in the comp.sources.unix
  531.       archives.  (A package named "kstuff" to help with kernel
  532.       rummaging was posted to alt.sources in May 1991 and is also
  533.       available via anonymous ftp as
  534.       usenet/alt.sources/articles/{329{6,7,8,9},330{0,1}}.Z from
  535.       wuarchive.wustl.edu.)
  536.  
  537. ------------------------------
  538.  
  539. Subject: How do I check the exit status of a remote command executed via "rsh"?
  540. Date: Thu Mar 18 17:16:55 EST 1993
  541.  
  542. 3.11) How do I check the exit status of a remote command
  543.       executed via "rsh" ?
  544.  
  545.       This doesn't work:
  546.  
  547.     rsh some-machine some-crummy-command || echo "Command failed"
  548.  
  549.       The exit status of 'rsh' is 0 (success) if the rsh program
  550.       itself completed successfully, which probably isn't what
  551.       you wanted.
  552.  
  553.       If you want to check on the exit status of the remote program,
  554.       you can try using Maarten Litmaath's 'ersh' script, which was
  555.       posted to alt.sources in January, 1991.  ersh is a shell script
  556.       that calls rsh, arranges for the remote machine to echo the
  557.       status of the command after it completes, and exits with that
  558.       status.
  559.  
  560. ------------------------------
  561.  
  562. Subject: Is it possible to pass shell variable settings into an awk program?
  563. Date: Thu Mar 18 17:16:55 EST 1993
  564.  
  565. 3.12) Is it possible to pass shell variable settings into an awk program?
  566.  
  567.       There are two different ways to do this.  The first involves
  568.       simply expanding the variable where it is needed in the program.
  569.       For example, to get a list of all ttys you're using:
  570.  
  571.     who | awk '/^'"$USER"'/ { print $2 }'                (1)
  572.  
  573.       Single quotes are usually used to enclose awk programs because
  574.       the character '$' is often used in them, and '$' will be
  575.       interpreted by the shell if enclosed inside double quotes, but
  576.       not if enclosed inside single quotes.  In this case, we *want*
  577.       the '$' in "$USER" to be interpreted by the shell, so we close
  578.       the single quotes and then put the "$USER" inside double quotes.
  579.       Note that there are no spaces in any of that, so the shell will
  580.       see it all as one argument.  Note, further, that the double
  581.       quotes probably aren't necessary in this particular case (i.e. we
  582.       could have done
  583.  
  584.     who | awk '/^'$USER'/ { print $2 }'                (2)
  585.  
  586.       ), but they should be included nevertheless because they are
  587.       necessary when the shell variable in question contains special
  588.       characters or spaces.
  589.  
  590.       The second way to pass variable settings into awk is to use an
  591.       often undocumented feature of awk which allows variable settings
  592.       to be specified as "fake file names" on the command line.  For
  593.       example:
  594.  
  595.     who | awk '$1 == user { print $2 }' user="$USER" -        (3)
  596.  
  597.       Variable settings take effect when they are encountered on the
  598.       command line, so, for example, you could instruct awk on how to
  599.       behave for different files using this technique.  For example:
  600.  
  601.     awk '{ program that depends on s }' s=1 file1 s=0 file2        (4)
  602.  
  603.       Note that some versions of awk will cause variable settings
  604.       encountered before any real filenames to take effect before the
  605.       BEGIN block is executed, but some won't so neither way should be
  606.       relied upon.
  607.  
  608.       Note, further, that when you specify a variable setting, awk
  609.       won't automatically read from stdin if no real files are
  610.       specified, so you need to add a "-" argument to the end of your
  611.       command, as I did at (3) above.
  612.  
  613. ------------------------------
  614.  
  615. Subject: How do I get rid of zombie processes that persevere?
  616. From: jik@rtfm.MIT.Edu (Jonathan I. Kamens)
  617. From: casper@fwi.uva.nl (Casper Dik)
  618. Date: Thu, 09 Sep 93 16:39:58 +0200
  619.  
  620. 3.13) How do I get rid of zombie processes that persevere?
  621.  
  622.       Unfortunately, it's impossible to generalize how the death of
  623.       child processes should behave, because the exact mechanism varies
  624.       over the various flavors of Unix.
  625.  
  626.       First of all, by default, you have to do a wait() for child
  627.       processes under ALL flavors of Unix.  That is, there is no flavor
  628.       of Unix that I know of that will automatically flush child
  629.       processes that exit, even if you don't do anything to tell it to
  630.       do so.
  631.  
  632.       Second, under some SysV-derived systems, if you do
  633.       "signal(SIGCHLD, SIG_IGN)" (well, actually, it may be SIGCLD
  634.       instead of SIGCHLD, but most of the newer SysV systems have
  635.       "#define SIGCHLD SIGCLD" in the header files), then child
  636.       processes will be cleaned up automatically, with no further
  637.       effort in your part.  The best way to find out if it works at
  638.       your site is to try it, although if you are trying to write
  639.       portable code, it's a bad idea to rely on this in any case.
  640.       Unfortunately, POSIX doesn't allow you to do this; the behavior
  641.       of setting the SIGCHLD to SIG_IGN under POSIX is undefined, so
  642.       you can't do it if your program is supposed to be
  643.       POSIX-compliant.
  644.  
  645.       So, what's the POSIX way? As mentioned earlier, you must
  646.       install a signal handler and wait. Under POSIX signal handlers
  647.       are installed with sigaction. Since you are not interested in
  648.       ``stopped'' children, only in terminated children, add SA_NOCLDSTOP
  649.       to sa_flags.  Waiting without blocking is done with waitpid().
  650.       The first argument to waitpid should be -1 (wait for any pid),
  651.       the third should be WNOHANG. This is the most portable way
  652.       and is likely to become more portable in future.
  653.  
  654.       If your systems doesn't support POSIX, there's a number of ways.
  655.       The easiest way is signal(SIGCHLD, SIG_IGN), if it works.
  656.       If SIG_IGN cannot be used to force automatic clean-up, then you've
  657.       got to write a signal handler to do it.  It isn't easy at all to
  658.       write a signal handler that does things right on all flavors of
  659.       Unix, because of the following inconsistencies:
  660.  
  661.       On some flavors of Unix, the SIGCHLD signal handler is called if
  662.       one *or more* children have died.  This means that if your signal
  663.       handler only does one wait() call, then it won't clean up all of
  664.       the children.  Fortunately, I believe that all Unix flavors for
  665.       which this is the case have available to the programmer the
  666.       wait3() or waitpid() call, which allows the WNOHANG option to
  667.       check whether or not there are any children waiting to be cleaned
  668.       up.  Therefore, on any system that has wait3()/waitpid(), your
  669.       signal handler should call wait3()/waitpid() over and over again
  670.       with the WNOHANG option until there are no children left to clean
  671.       up. Waitpid() is the preferred interface, as it is in POSIX.
  672.  
  673.       On SysV-derived systems, SIGCHLD signals are regenerated if there
  674.       are child processes still waiting to be cleaned up after you exit
  675.       the SIGCHLD signal handler.  Therefore, it's safe on most SysV
  676.       systems to assume when the signal handler gets called that you
  677.       only have to clean up one signal, and assume that the handler
  678.       will get called again if there are more to clean up after it
  679.       exits.
  680.  
  681.       On older systems, there is no way to prevent signal handlers
  682.       from being automatically reset to SIG_DFL when the signal
  683.       handler gets called.  On such systems, you have to put
  684.       "signal(SIGCHILD, catcher_func)" (where "catcher_func" is the
  685.       name of the handler function) as the last thing in the signal
  686.       handler, so that it gets reset.
  687.  
  688.       Fortunately, newer implementations allow signal handlers to be
  689.       installed without being reset to SIG_DFL when the handler
  690.       function is called.  To get around this problem, on systems that
  691.       do not have wait3()/waitpid() but do have SIGCLD, you need to
  692.       reset the signal handler with a call to signal() after doing at
  693.       least one wait() within the handler, each time it is called.  For
  694.       backward compatibility reasons, System V will keep the old
  695.       semantics (reset handler on call) of signal().  Signal handlers
  696.       that stick can be installed with sigaction() or sigset().
  697.  
  698.       The summary of all this is that on systems that have waitpid()
  699.       (POSIX) or wait3(), you should use that and your signal handler
  700.       should loop, and on systems that don't, you should have one call
  701.       to wait() per invocation of the signal handler.
  702.  
  703.       One more thing -- if you don't want to go through all of this
  704.       trouble, there is a portable way to avoid this problem, although
  705.       it is somewhat less efficient.  Your parent process should fork,
  706.       and then wait right there and then for the child process to
  707.       terminate.  The child process then forks again, giving you a
  708.       child and a grandchild.  The child exits immediately (and hence
  709.       the parent waiting for it notices its death and continues to
  710.       work), and the grandchild does whatever the child was originally
  711.       supposed to.  Since its parent died, it is inherited by init,
  712.       which will do whatever waiting is needed.  This method is
  713.       inefficient because it requires an extra fork, but is pretty much
  714.       completely portable.
  715.  
  716. ------------------------------
  717.  
  718. Subject: How do I get lines from a pipe ... instead of only in larger blocks?
  719. From: jik@rtfm.MIT.Edu (Jonathan I. Kamens)
  720. Date: Sun, 16 Feb 92 20:59:28 -0500
  721.  
  722. 3.14) How do I get lines from a pipe as they are written instead of only in
  723.       larger blocks?
  724.  
  725.       The stdio library does buffering differently depending on whether
  726.       it thinks it's running on a tty.  If it thinks it's on a tty, it
  727.       does buffering on a per-line basis; if not, it uses a larger
  728.       buffer than one line.
  729.  
  730.       If you have the source code to the client whose buffering you
  731.       want to disable, you can use setbuf() or setvbuf() to change the
  732.       buffering.
  733.  
  734.       If not, the best you can do is try to convince the program that
  735.       it's running on a tty by running it under a pty, e.g. by using
  736.       the "pty" program mentioned in question 3.9.
  737.  
  738. ------------------------------
  739.  
  740. End of unix/faq Digest part 3 of 7
  741. **********************************
  742.  
  743. -- 
  744. Ted Timar - tmatimar@isgtec.com
  745. ISG Technologies Inc., 6509 Airport Road, Mississauga, Ontario, Canada L4V 1S7
  746.  
  747.